﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Threading;

namespace app
{
    public class Watch : INotifyPropertyChanged
    {
        private string time;
        DispatcherTimer timer = new DispatcherTimer();

        DrawImage pimg = new DrawImage(100, 100, 4);
        ImageSource watchImg;

        

        public Watch()
        {
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += timer_Tick;


            pimg.initialize(pimg.Width, pimg.Height, 4);
            pimg.CreateColorImageData(pimg.Width, pimg.Height, pimg.ImgData);
            timer.Tick += imageRender_Tick;
        }

        internal void startWatch() { timer.Start(); }
        internal void stopWatch() { timer.Stop(); }

        void timer_Tick(object sender, EventArgs e)
        {
            //Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");//ToString("yyyy/MM/dd HH:mm:ss");//DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
            //여기도 Time로 접근!
        }

        void imageRender_Tick(object sender, EventArgs e)   //타이머에 따라 이미지 렌더링하기
        {
            byte[] buf = new byte[pimg.Width * 4];
            Random ran = new Random();

            for (int col = 0; col < pimg.Width; col++)
            {
                buf[col * 4 + 0] = pimg.ImgData[(pimg.Height - 1) * pimg.Width * 4 + col * 4 + 0];
                buf[col * 4 + 1] = pimg.ImgData[(pimg.Height - 1) * pimg.Width * 4 + col * 4 + 1];
                buf[col * 4 + 2] = pimg.ImgData[(pimg.Height - 1) * pimg.Width * 4 + col * 4 + 2];
                buf[col * 4 + 3] = pimg.ImgData[(pimg.Height - 1) * pimg.Width * 4 + col * 4 + 3];
            }

            for (int col = 0; col < pimg.Width; col++)
            {
                pimg.ImgData[pimg.Width * 4 + col * 4 + 0] = buf[col * 4 + 0];
                pimg.ImgData[pimg.Width * 4 + col * 4 + 1] = buf[col * 4 + 1];
                pimg.ImgData[pimg.Width * 4 + col * 4 + 2] = buf[col * 4 + 2];
                pimg.ImgData[pimg.Width * 4 + col * 4 + 3] = buf[col * 4 + 3];
            }


            for (int row = 0; row < pimg.Height; row++)
            {
                for (int col = 0; col < pimg.Width; col++)
                {
                    pimg.ImgData[row * pimg.Width * 4 + col * 4 + 0] = (byte)ran.Next(0, 255);  // B
                    pimg.ImgData[row * pimg.Width * 4 + col * 4 + 1] = (byte)ran.Next(0, 255);  // G
                    pimg.ImgData[row * pimg.Width * 4 + col * 4 + 2] = (byte)ran.Next(0, 255);  // R
                    pimg.ImgData[row * pimg.Width * 4 + col * 4 + 3] = 0xFF;                     // A는 0xFF로 고정
                }
            }

            pimg.ImageDataToBitmap();
            pimg.WriteableBitmapToImageSource();

            WatchImg = pimg.ImgSource;

            
        }

        public ImageSource WatchImg
        {
            get { return watchImg; }
            set { watchImg = value; OnPropertyChanged("WatchImg"); }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public DrawImage Pimg
        {
            get { return pimg; }
            set { pimg = value; }
        }

        //public string Time
        //{
        //    get { return time; }
        //    set { time = value; OnPropertyChanged("Time"); }
        //}

        //private void OnPropertyChanged(string propertyName)
        //{
        //    if (PropertyChanged != null)
        //        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        //}
        //public event PropertyChangedEventHandler PropertyChanged;
    }
}

        